<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>时间日期</title>
    <script>
        function startTime() {
            var today = new Date();
            const year = today.getFullYear();
            const month = today.getMonth() + 1;
            const day = today.getDate();
            var h = today.getHours() > 12 ? today.getHours() - 12:today.getHours();
            var m = today.getMinutes();
            var s = today.getSeconds();// 在小于10的数字前加一个‘0’
            const a = today.getHours() > 12 ? 'PM' : 'AM';

            var str = "";
            var week = today.getDay();
            if (week == 0) {
                str = "星期日";
            } else if (week == 1) {
                str = "星期一";
            } else if (week == 2) {
                str = "星期二";
            } else if (week == 3) {
                str = "星期三";
            } else if (week == 4) {
                str = "星期四";
            } else if (week == 5) {
                str = "星期五";
            } else if (week == 6) {
                str = "星期六";
            }
            m = checkTime(m);
            s = checkTime(s);
            document.getElementById('txt').innerHTML =
                year + "年" + month + "月" + day + "日"
                + h + ":" + m + ":" + s
                + " " + a + " " + str;
            t = setTimeout(function () {
                startTime()
            }, 500);
        }

        function checkTime(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }
    </script>
</head>
<body onload="startTime()">
<div>现在是北京时间：</div>
<div id="txt"></div>

</body>
</html>
